package net.huashitong.supervision.gongjian.service.project;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.gongjian.dao.KaiGongDao;
import net.huashitong.supervision.gongjian.entity.project.KaiGongEntity;
import net.huashitong.supervision.gongjian.entity.project.ZhengQianEntity;
import net.huashitong.supervision.gongjian.filter.KaiGongFilter;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectFeedBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KaiGongService extends StringPKService<KaiGongEntity> {
    @Autowired
    private KaiGongDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private ProjectFeedBackService feedBackService;
    @Override
    protected BaseDao<KaiGongEntity, String> getDao() {
        return dao;
    }


    @Override
    protected void beforeSave(KaiGongEntity obj) {
        String sysId = tableService.updateMaxSysId("t_gj_kaigongtisu", null);
        obj.setSysId(sysId);
    }

    private KaiGongEntity setState(KaiGongEntity entity, int vaild , int pType){
        int feedbackCount = feedBackService.getFeedbackCount(entity.getSysId(), vaild, pType);
        if(1<=feedbackCount){
            entity.setCurrentBack(1);
        }else {
            entity.setCurrentBack(0);
        }
        return entity;
    }


    public List<KaiGongEntity> getList(KaiGongFilter filter)
    {
        List<KaiGongEntity> list = dao.getList(filter);
        for (KaiGongEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 2);
            setState(entity,1,2);
            entity.setZerenDept(projectDeptName);
        }

        return list;
    }
    public List<KaiGongEntity> getListForOwn(KaiGongFilter filter)
    {
        filter.setProjectType(2);
        List<KaiGongEntity> list = dao.getListForOwn(filter);
        for (KaiGongEntity entity : list) {
            String projectDeptName = deptProjectService.getProjectDeptName(entity.getSysId(), 2, 2);
            setState(entity,1,2);

            entity.setZerenDept(projectDeptName);
        }

        return list;
    }

    public List<KaiGongEntity> getListNoPage(KaiGongFilter filter)
    {
        List<KaiGongEntity> list = dao.getListNopage(filter);


        return list;
    }

}
